//
//  PickImageView.m
//  WindRunner
//
//  Created by leeqb on 11/26/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "PickImageView.h"

@implementation PickImageView

- (id)init
{
    self = [super init];
    if(self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _options =  @[@"拍照", @"相册选取", @"取消"];
    }
    return self;
}

- (void)show
{
    CGRect rect = CGRectZero;
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = SCREEN_WIDTH;
    rect.size.height = SCREEN_HEIGHT;
    UIView *blackView = [[UIView alloc] initWithFrame:rect];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.alpha = 0.5;
    [self addSubview:blackView];
    
    rect.size.width = SCREEN_WIDTH - 60;
    rect.size.height = _options.count * 44;
    rect.origin.x = (SCREEN_WIDTH - rect.size.width) / 2;
    rect.origin.y = (SCREEN_HEIGHT - rect.size.height) / 2;
    FMTableView *optionTable = [[FMTableView alloc] initWithFrame:rect];
    optionTable.delegate = self;
    optionTable.dataSource = self;
    optionTable.scrollEnabled = NO;
    optionTable.layer.cornerRadius = 4;
    optionTable.layer.masksToBounds = YES;
    optionTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:optionTable];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [blackView addGestureRecognizer:tapGesture];
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)hide
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _options.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"OptionSelectCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if(!cell) {
        cell = [[FMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = RGB(109, 195, 255);
        
        CGRect rect = CGRectZero;
        rect.origin.x = 10;
        rect.size.width = tableView.frame.size.width - rect.origin.x*2;
        rect.size.height = 44;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:rect];
        titleLabel.tag = 1;
        titleLabel.font = [UIFont boldSystemFontOfSize:15];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [cell addSubview:titleLabel];
    }
    
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:1];
    titleLabel.text = _options[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    UIImagePickerController *imagePickerVC = nil;
    if(indexPath.row == 0) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            imagePickerVC = [[UIImagePickerController alloc] init];
            imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerVC.showsCameraControls = YES;
        }
    } else if(indexPath.row == 1) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            imagePickerVC = [[UIImagePickerController alloc] init];
            imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
    }
    
    if(imagePickerVC) {
        imagePickerVC.allowsEditing = YES;
        
        if(_delegate && [_delegate respondsToSelector:@selector(imageTypeSelected:)]) {
            [_delegate imageTypeSelected:imagePickerVC];
        }
    }
    
    [self hide];
}

@end
